package com.jinke.uang.ui.activity.validation;

import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.jinke.uang.R;
import com.jinke.uang.base.BaseNormalActivity;
import com.jinke.uang.base.BaseResponse;
import com.jinke.uang.bean.PersonInfo;
import com.jinke.uang.common.RouterMap;
import com.jinke.uang.common.NoDoubleClickListener;
import com.jinke.uang.common.UserAuthRouter;
import com.jinke.uang.common.UserInfo;
import com.jinke.uang.constant.Constant;
import com.jinke.uang.bean.BindedCardData;
import com.jinke.uang.constant.HttpConstant;
import com.jinke.uang.model.AuthModule;
import com.jinke.uang.presenter.CommonPresenter;
import com.jinke.uang.bean.DictionaryItemData;
import com.jinke.uang.utils.StringUtils;
import com.jinke.uang.utils.ToastUtils;
import com.jinke.uang.view.MenuDialog;


import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

@Route(name = "银行卡认证", path = RouterMap.BANK_AUTH_PAGE)
public class BankAuthActivity extends BaseNormalActivity<CommonPresenter,AuthModule> {

    @Autowired(name = "is_change")
    public boolean is_change;
    @BindView(R.id.topBack)
    ImageView topBack;
    @BindView(R.id.topText)
    TextView topText;
    @BindView(R.id.etName)
    EditText etName;
    @BindView(R.id.etCardNum)
    EditText etCardNum;
    @BindView(R.id.etBankName)
    TextView etBankName;
    @BindView(R.id.rlBankName)
    RelativeLayout rlBankName;
    @BindView(R.id.tvNext)
    TextView tvNext;
    private ArrayList<DictionaryItemData>mBankList;
    private MenuDialog mMenuDialog;

    @Override
    protected void initViews() {
        ButterKnife.bind(this);

        topText.setText(getStringResource(R.string.bank_card_page_title));

        topBack.setOnClickListener(viewclick);
        tvNext.setOnClickListener(viewclick);
        etBankName.setOnClickListener(viewclick);
        rlBankName.setOnClickListener(viewclick);
    }

    @Override
    protected void onUIReady() {
        ARouter.getInstance().inject(this);
        mPresenter.getData(HttpConstant.DICTIONARY_DATA,null,"common_bankname");
        if (UserInfo.isShowUserInformation()) {
            mPresenter.getData(HttpConstant.BINDED_CARD_DATA,null);
        }
        mPresenter.getData(HttpConstant.PERSON_INFO,null);
    }

    /**
     * 点击信息
     */
    private NoDoubleClickListener viewclick = new NoDoubleClickListener() {
        @Override
        public void onNoDoubleClick(View v) {
            super.onNoDoubleClick(v);
            switch (v.getId()) {
                case R.id.topBack:
                    finish();
                    break;
                case R.id.tvNext:
                    submit();
                    break;
                case R.id.etBankName:
                case R.id.rlBankName:
                    pickerBank();
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected int getContentResId() {
        return R.layout.activity_auth_bankcard;
    }

    @Override
    protected int getResId() {
        return 0;
    }

    @Override
    public String getPageTitle() {
        return null;
    }

    @Override
    protected CommonPresenter getPresenter() {
        return new CommonPresenter();
    }

    @Override
    protected AuthModule getModule() {
        return new AuthModule();
    }

    private void submit() {
        tvNext.setClickable(false);
        String realName = etName.getText().toString().trim();
        String bankCard = etCardNum.getText().toString().trim();
        String bankName = etBankName.getText().toString().trim();
        if(StringUtils.isEmpty(realName)){
            ToastUtils.showShort(BankAuthActivity.this,getStringResource(R.string.bank_card_bind_name_hint));
            tvNext.setClickable(true);
            return;
        }
        if(StringUtils.isEmpty(bankCard)){
            ToastUtils.showShort(BankAuthActivity.this,getStringResource(R.string.bank_card_bind_banknum_hint));
            tvNext.setClickable(true);
            return;
        }
        if(StringUtils.isEmpty(bankName)){
            ToastUtils.showShort(BankAuthActivity.this,getStringResource(R.string.bank_card_bind_bankname_hint));
            tvNext.setClickable(true);
            return;
        }
        String bankCode = etBankName.getTag().toString();
        mPresenter.getData(HttpConstant.SAVE_BANK_CARD_INFO,null,realName,bankCode,bankName,bankCard);
        mLoading.showLoading();
    }

    private void fillTextView(BindedCardData data) {
        if (etName != null && !StringUtils.isEmpty(data.getRealName())) {
            etName.setText(data.getRealName());
        }
        if (etBankName != null && !StringUtils.isEmpty(data.getBankCode())) {
            etBankName.setTag(data.getBankCode());
        }
        if (etCardNum != null && !StringUtils.isEmpty(data.getBankCode())) {
            etCardNum.setText(data.getBankCard());
        }
        if (etBankName != null && !StringUtils.isEmpty(data.getBankValue())) {
            etBankName.setText(data.getBankValue());
        }
    }

    public MenuDialog getMenuDialog() {
        if (mMenuDialog == null) {
            mMenuDialog = new MenuDialog(this, R.style.dialog_bottom_full);
        }
        return mMenuDialog;
    }

    private void pickerBank() {
        getMenuDialog().showDialog(etBankName, mBankList);
    }


    @Override
    public void showResponse(int apiType,String listType, Object o) {
        dismissLoadingView();

        if(apiType== HttpConstant.BINDED_CARD_DATA){//获取银行卡信息接口
            BaseResponse<BindedCardData> response = (BaseResponse<BindedCardData>) o;
            fillTextView(response.getData());
        }

        else if(apiType==HttpConstant.SAVE_BANK_CARD_INFO){ //添加银行卡接口
            BaseResponse<String> response = (BaseResponse<String>) o;
            if (response.getStatus() == Constant.HTTP_OK) {
                if (!is_change) {
                    UserAuthRouter.getInstance().jumpNextPager(this, RouterMap.BANK_AUTH_PAGE);
                } else {
                    finish();
                }
            } else {
                ToastUtils.showShort(this, response.getMessage());
            }
            tvNext.setClickable(true);
        }

        else if(apiType==HttpConstant.PERSON_INFO){  //用户信息回显
            BaseResponse<PersonInfo> response = (BaseResponse<PersonInfo>) o;
            if (response.getStatus() == Constant.HTTP_OK) {
                etName.setText(response.getData().getRealName());
            }
        }

        else if(apiType==HttpConstant.DICTIONARY_DATA){ //获取字典信息
            BaseResponse<ArrayList<DictionaryItemData>> response = (BaseResponse<ArrayList<DictionaryItemData>>) o;
            ArrayList<DictionaryItemData> data = response.getData();
                mBankList = data;
        }
    }

    @Override
    public void showError(Throwable e) {
        tvNext.setClickable(true);
        dismissLoadingView();
    }
}
